<template>
  <div class="app-container">
    <!-- 科室情况 -->
    <el-row>
      <el-select v-model="query.time" placeholder="请选择" style="width: 80px">
        <el-option
          v-for="item in [
            {
              label: '出院',
              value: 'cysj_l',
            },
            {
              label: '入院',
              value: 'rysj_l',
            },
            {
              label: '结算',
              value: 'jsrq_l',
            },
          ]"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>

      <el-date-picker
        v-model="query.stime"
        value-format="yyyy-MM-dd"
        type="date"
        placeholder="开始日期"
        style="margin-left: 5px"
      >
      </el-date-picker>
      <span style="margin-left: 2px">至</span>
      <el-date-picker
        v-model="query.etime"
        value-format="yyyy-MM-dd"
        type="date"
        placeholder="结束日期"
        style="margin-left: 5px"
      >
      </el-date-picker>

      <el-select
        style="margin-left: 5px; width: 250px"
        v-model="query.ylfkfs"
        multiple
        collapse-tags
        filterable
        placeholder="医疗付款方式"
      >
        <el-option
          v-for="item in ykfs"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
      <el-input
        size="medium"
        placeholder="科室"
        suffix-icon="el-icon-search"
        style="width: 250px; margin-left: 5px"
        v-model="query.s"
        @keyup.enter.native="search"
      ></el-input>
      <el-button style="margin-left: 5px" type="primary" round @click="search"
        >查询</el-button
      >
    </el-row>

    <el-row>
      <el-table
        :data="ls2"
        style="width: 100%; margin-top: 5px"
        v-loading="loading"
        @sort-change="sortChange"
        border
      >
        <el-table-column
          prop="cykb"
          label="出院科室"
          width="200"
          fixed
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="z_docCount"
          :sortable="true"
          fixed
          label="人次"
          align="center"
        >
        </el-table-column>
        <el-table-column align="center" label="入组情况">
          <el-table-column
            prop="l_mdccount"
            label="MDC数量"
            width="120"
            align="center"
          >
          </el-table-column>

          <el-table-column
            prop="l_drgcount"
            label="DRG数量"
            width="120"
            align="center"
          >
          </el-table-column>

          <el-table-column
            prop="l_weight_sum"
            label="总点数"
            :formatter="fmt"
            align="center"
            width="120"
          >
          </el-table-column>

          <el-table-column
            prop="l_weight_avg"
            label="CMI"
            align="center"
            width="120"
          >
            <template slot-scope="scope">
              {{ (scope.row.l_weight_avg / 100).toFixed(4) }}
            </template>
          </el-table-column>

          <el-table-column
            prop="l_sjzyts_l_avg"
            label="平均床日"
            align="center"
            :formatter="fmt"
            width="120"
          >
          </el-table-column>

          <el-table-column
            prop="l_benchmarkFee_sum"
            label="标准总费用"
            :formatter="fmt"
            align="center"
            width="150"
            :sortable="true"
          >
          </el-table-column>

          <el-table-column
            prop="l_zfy_sum"
            label="总住院费用"
            align="center"
            :formatter="fmt"
            width="120"
          >
          </el-table-column>

          <!-- 新增DRG统筹、基金支付总额 -->
          <el-table-column
            label="基金支付总额"
            align="center"
            width="120"
          >
          <template slot-scope="scope">
            {{ (scope.row.jjzfze || 0).toFixed(2) }}
          </template>
          </el-table-column>
          <el-table-column
            prop="drgtc"
            label="DRG统筹"
            align="center"
            width="120"
            :formatter="fmt"
          >
          </el-table-column>
         <!-- 费用差  修改：DRG统筹 - 基金支付总额 -->
          <el-table-column label="费用差" prop="费用差" :formatter="费用差fmt" align="center" width="150">
          </el-table-column>

          <el-table-column
            prop="l_zfy_avg"
            label="平均费用"
            align="center"
            :formatter="fmt"
            width="120"
          >
          </el-table-column>

          <el-table-column
            prop="l_wk"
            width="120"
            label="手术组"
            align="center"
          >
          </el-table-column>

          <el-table-column
            prop="l_cz"
            width="120"
            label="非手术室治疗组"
            align="center"
          >
          </el-table-column>
          <el-table-column
            prop="l_nk"
            width="120"
            label="内科组"
            align="center"
          >
          </el-table-column>

          <el-table-column prop="gdbl_1" label="高倍率" align="center">
          </el-table-column>
          <el-table-column prop="gdbl_-1" label="低倍率" align="center">
          </el-table-column>
        </el-table-column>
        <el-table-column align="center" label="整体情况">
          <el-table-column
            prop="z_ingrp"
            :sortable="true"
            label="入组数"
            width="100"
            align="center"
          >
          </el-table-column>
          <el-table-column
            prop="入组占比"
            label="入组占比%"
            width="100"
            :formatter="入组占比"
            align="center"
          >
          </el-table-column>
          <el-table-column
            prop="z_notGrp"
            :sortable="true"
            label="未入组数"
            width="120"
            align="center"
          >
          </el-table-column>
          <el-table-column
            prop="未入组占比"
            :formatter="入组占比"
            label="未入组占比%"
            width="120"
            align="center"
          >
          </el-table-column>
          <el-table-column
            prop="z_zfy_sum"
            :sortable="true"
            :formatter="fmt"
            label="总住院费"
            width="120"
            align="center"
          >
          </el-table-column>

          <el-table-column
            prop="z_zfy_avg"
            :sortable="true"
            label="平均住院费"
            :formatter="fmt"
            width="120"
            align="center"
          >
          </el-table-column>
          <el-table-column
            prop="z_sjzyts_l_avg"
            :sortable="true"
            label="平均床日"
            width="120"
            :formatter="fmt"
            align="center"
          >
          </el-table-column>
          <el-table-column
            prop="z_dead_sum"
            label="死亡人数"
            width="120"
            align="center"
          >
          </el-table-column>
          <el-table-column
            label="死亡率"
            width="120"
            prop="死亡率"
            align="center"
            :formatter="入组占比"
          >
          </el-table-column>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          @size-change="sizeChange"
          @current-change="curChange"
          :current-page="query.page"
          :page-sizes="[5, 10, 20, 50, 100, 200, 300, 400]"
          :page-size="query.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="query.totalElements"
        >
        </el-pagination>
      </div>
    </el-row>
  </div>
</template>

<script lang="ts">
import { Component, Vue } from "vue-property-decorator";
import { UserModule } from "@/store/modules/user";
import { pfmls } from "@/api/cloudwebapi";
import Pagination from "@/components/Pagination/index.vue";
import { cloneDeep } from "lodash";
import { Form } from "element-ui";
import { parseTime, dateFormat } from "@/utils";
import { dicList } from "@/api/datatranform";
@Component({
  name: "HospCykb",
  components: {
    Pagination,
  },
})
export default class extends Vue {
  private query = {
    s: "",
    stime: "",
    etime: dateFormat(new Date(), "yyyy-MM-dd"),
    time: "jsrq_l",
    grps: "cykb",
    page: 1,
    size: 10,
    wh: [],
    totalElements: 0,
    ylfkfs: "",
  };
  private loading: boolean = true;

  private 入组占比(r: any, c: any) {
    const { row, column } = r;
    if (c.property == "入组占比") {
      return ((r.z_ingrp / r.z_docCount) * 100).toFixed(2);
    }
    if (c.property == "未入组占比") {
      return ((r.z_notGrp / r.z_docCount) * 100).toFixed(2);
    }
    if (c.property == "死亡率") {
      return ((r.z_dead_sum / r.z_docCount) * 100).toFixed(2);
    }
    return "";
  }
  private async sortChange(p: any) {
    let order = p.order;
    let prop = p.prop;
    let x = order == "ascending" ? 1 : -1;

    let ts: any = JSON.parse(JSON.stringify(this.ls));

    ts = ts.sort((a: any, b: any) => {
      if (isNaN(a[prop])) {
        return a[prop].localeCompare(b[prop]) * x;
      } else {
        return (a[prop] - b[prop]) * x;
      }
    });
    this.ls = JSON.parse(JSON.stringify(ts));
    ts = ts.slice(
      (this.query.page - 1) * this.query.size,
      this.query.page * this.query.size
    );
    this.ls2 = ts;
  }

  private search() {
    this.loadPfmls();
  }

  private sizeChange(val: any) {
    this.query.size = val;
  }

  private curChange(val: any) {
    this.query.page = val;
    this.ls2 = this.ls.slice(
      (this.query.page - 1) * this.query.size,
      this.query.page * this.query.size
    );
  }

  // 修改费用差计算方式
  private 费用差fmt(row: any) {
    // 修改：DRG统筹 - 基金支付总额
    // console.log('rdrg', row.drgtc, row.jjzfze)
    // row['费用差'] = (row.drgtc - row.jjzfze).toFixed(2)
    if (row.drgtc && row.jjzfze) {
      row['费用差'] = (row.drgtc - row.jjzfze).toFixed(2)
    } else {
      row['费用差'] = ''
    }

    return row['费用差']
  }

  private fmt(row: any, c: any, val: any) {
    let p = c.property;

    if (isNaN(val)) return val;

    if (["weight_sum", "weight_avg"].includes(p)) {
      return val.toFixed(4);
    } else {
      try {
        return val.toFixed(2);
      } catch (e) {
        return val;
      }
    }
    //return val;
  }

  private dicLs: Array<any> = [];
  private ykfs: Array<any> = [];
  private async loadDicLs() {
    const { data } = await dicList({ size: 1000 });
    return data.content;
  }
  private ls: Array<any> = [];
  private ls2: Array<any> = [];
  private async loadPfmls() {
    this.loading = true;
    let q: any = cloneDeep(this.query);
    q.stime = q.stime.replaceAll("-", "");
    q.etime = q.etime.replaceAll("-", "");
    if (q.ylfkfs && q.ylfkfs.length > 0) {
      q.ylfkfs = q.ylfkfs.join(",");
    }
    if (q.s) {
      q.s = q.s.toLowerCase();
      q.wh.push({ f: this.query.grps, op: "match", vals: q.s });
    }
    let q1 = cloneDeep(q);
    q1.wh = JSON.stringify(q1.wh);
    const { data } = await pfmls(q1);
    if (data) {
      data.forEach((it: any) => {
        let keys = Object.keys(it).filter((it: any) => it !== this.query.grps);
        for (let k of keys) {
          it[`z_${k}`] = it[k];
        }
        it["z_notGrp"] = it.docCount - it.ingrp;
      });
    }
    if (q.wh) {
      q.wh.push({ f: "ingrp", op: "eq", vals: true });
    }
    q.wh = JSON.stringify(q.wh);
    let d1: any = await pfmls(q);
    d1 = d1.data;
    if (d1) {
      d1.forEach((it: any) => {
        let keys = Object.keys(it);
        for (let k of keys) {
          it[`l_${k}`] = it[k];
        }
        it["l_notGrp"] = it.docCount - it.ingrp;
      });
    }

    //属性合并
    data.forEach((t1: any) => {
      let ts = d1.filter(
        (t2: any) => t1[this.query.grps] == t2[this.query.grps]
      );
      if (ts.length > 0) {
        ts = ts[0];
        Object.keys(ts).forEach((k: any) => {
          t1[k] = ts[k];
        });
      }
    });

    this.query.totalElements = data.length;

    this.ls = data;
    if (this.ls) {
      this.ls2 = this.ls.slice(0, this.query.size);
    }
    this.loading = false;
  }

  async created() {
    let cDate = new Date();
    // cDate.setMonth(cDate.getMonth() - 3);
    cDate.setMonth(cDate.getMonth()) // 修改默认开始时间为当月1号
    cDate.setDate(1)
    this.query.stime = dateFormat(cDate, "yyyy-MM-dd");
    this.dicLs = await this.loadDicLs();

    this.ykfs = this.dicLs
      .filter((it: any) => it.category == "医疗付款方式代码")
      .map((it: any) => ({ label: it.name, value: it.code }));
    this.loadPfmls();
  }
}
</script>

<style lang="scss">
.el-date-editor {
  width: 140px !important;
}
.error {
  color: red;
}
.pagination-container {
  float: right;
}
.el-dialog__body {
  padding: 10px !important;
}
</style>